from datetime import datetime
from app import db


class UserPhoto(db.Model):
    """用户照片表"""
    __tablename__ = 'user_photos'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, comment='用户ID')
    photo_url = db.Column(db.String(255), nullable=False, comment='照片URL')
    is_avatar = db.Column(db.Boolean, default=False, comment='是否为头像')
    sort_order = db.Column(db.Integer, default=0, comment='排序')
    status = db.Column(db.Integer, default=1, comment='状态：1待审核2通过3拒绝')
    audit_reason = db.Column(db.String(200), comment='审核原因')
    
    # 时间戳
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'photo_url': self.photo_url,
            'is_avatar': self.is_avatar,
            'sort_order': self.sort_order,
            'status': self.status,
            'status_text': self.get_status_text(),
            'audit_reason': self.audit_reason,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None
        }
    
    def get_status_text(self):
        """获取状态文本"""
        status_map = {1: '待审核', 2: '通过', 3: '拒绝'}
        return status_map.get(self.status, '未知')
    
    def __repr__(self):
        return f'<UserPhoto {self.id}>' 